home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung / Power-Programmierung (Tewi)(1994).iso / magazine / msysjour / vol06 / 04 / winstdio / simple.c < prev    next >
C/C++ Source or Header  |  1991-07-01  |  3KB  |  121 lines

  1. /* 
  2. SIMPLE.C -- not so simple, really
  3.  
  4. Microsoft C, Windows SDK:
  5. cl -c -AS -Gsw -Oais -Zpe simple.c
  6. link /align:16 simple,simple,nul,/nod slibcew libw,simple.def
  7. rc simple.exe
  8.  
  9. ; SIMPLE.DEF
  10. NAME        SIMPLE
  11. EXETYPE     WINDOWS
  12. CODE        PRELOAD MOVEABLE DISCARDABLE
  13. DATA        PRELOAD MOVEABLE MULTIPLE
  14. HEAPSIZE    4096
  15. STACKSIZE   8192
  16. EXPORTS     WndProc
  17.  
  18. Borland C++ (no .DEF file required):
  19. bcc -WS -G -O -Z -w-par -Hu simple.c
  20. rc simple.exe
  21. */
  22.  
  23. #include <stdarg.h>
  24. #include "windows.h"
  25.  
  26. static unsigned height;
  27.  
  28. int hprintf(HDC hdc, int linenum, const char *fmt, ...)
  29. {
  30.     static char s[256];
  31.     int len;
  32.     va_list marker;
  33.     va_start(marker, fmt);
  34.     TextOut(hdc, 0, linenum * height, s,
  35.         len = wvsprintf(s, fmt, marker));
  36.     va_end(marker);
  37.     return len;
  38. }
  39.  
  40. long FAR PASCAL _export WndProc(HWND hwnd, WORD message, 
  41.     WORD wParam, LONG lParam)
  42. {
  43.     HDC hdc;
  44.     PAINTSTRUCT ps;
  45.     TEXTMETRIC tm;
  46.     unsigned vers, flags;
  47.     int i;
  48.     
  49.     switch (message)
  50.     {
  51.         case WM_CREATE:
  52.             hdc = GetDC(hwnd);
  53.             GetTextMetrics(hdc, &tm);
  54.             height = tm.tmHeight + tm.tmExternalLeading;
  55.             ReleaseDC(hwnd, hdc);
  56.             return 0;
  57.             
  58.         case WM_PAINT:
  59.             vers = GetVersion();
  60.             flags = GetWinFlags();
  61.             hdc = BeginPaint(hwnd, &ps);
  62.             i = 0;
  63.             hprintf(hdc, i++, "Windows v. %d.%d", 
  64.                 LOBYTE(vers), HIBYTE(vers));
  65.             hprintf(hdc, i++, "%s mode", (char far *)
  66.                 ((flags & WF_ENHANCED) ? "Enhanced" :
  67.                 (flags & WF_STANDARD)  ? "Standard" :
  68.                 /* default */            "Real"));
  69.             hprintf(hdc, i++, "%d tasks running", 
  70.                 GetNumTasks());
  71.             EndPaint(hwnd, &ps);
  72.             return 0;
  73.             
  74.         case WM_DESTROY:
  75.             PostQuitMessage(0);
  76.             return 0;
  77.             
  78.         default:
  79.             return DefWindowProc(hwnd, message, wParam, lParam);
  80.     }
  81. }
  82.  
  83. int PASCAL WinMain(HANDLE hInstance, HANDLE hPrevInstance,
  84.     LPSTR lpszCmdParam, int nCmdShow)
  85. {
  86.     HWND hwnd;
  87.     MSG msg;
  88.     WNDCLASS wndclass;
  89.     
  90.     if (! hPrevInstance)
  91.     {
  92.         wndclass.style = CS_HREDRAW | CS_VREDRAW | CS_BYTEALIGNCLIENT;
  93.         wndclass.lpfnWndProc = WndProc;
  94.         wndclass.cbClsExtra = 0;
  95.         wndclass.cbWndExtra = 0;
  96.         wndclass.hInstance = hInstance;
  97.         wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
  98.         wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
  99.         wndclass.hbrBackground = GetStockObject(WHITE_BRUSH);
  100.         wndclass.lpszMenuName = NULL;
  101.         wndclass.lpszClassName = "test";
  102.         RegisterClass(&wndclass);
  103.     }
  104.     
  105.     hwnd = CreateWindow("test", "TEST", WS_OVERLAPPEDWINDOW,
  106.         CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
  107.         NULL, NULL, hInstance, NULL);
  108.     
  109.     ShowWindow(hwnd, nCmdShow);
  110.     UpdateWindow(hwnd);
  111.     
  112.     while (GetMessage(&msg, NULL, 0, 0))
  113.     {
  114.         TranslateMessage(&msg);
  115.         DispatchMessage(&msg);
  116.     }
  117.     
  118.     return msg.wParam;
  119. }
  120.  
  121.